package com.bindapp.http;

import android.content.Context;

import com.bindapp.App;
import com.bindapp.bean.EzTokenBean;
import com.bindapp.listener.HttpListener;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bindapp.util.Static.EzTokenUrl;

/**
 * Created by tom055 on 2018/6/24.
 */

public class PunFuncRequest {

    /**
     * 请求ez token
     */
    public static void requestEzToken(final Context context,final HttpListener result){
        MNetHttp.getInstance(context).rxGetRequest(EzTokenUrl, EzTokenBean.class, new HttpListener() {
            @Override
            public void success(Object obj) {
                String token = ((EzTokenBean)obj).getData().getAccessToken();
                App.getOpenSDK().setAccessToken(token);
                PreferencesUtil.getInstance(context).setEzToken(token);
                result.success(obj);

            }

            @Override
            public void fail(String msg) {
                result.fail(msg);
            }
        });
    }
    //获取列表 1:LOAD_MY_DEVICE=0 LOAD_SHARE_DEVICE=1 2:true false
    public static void getEzList(int type,int page,final HttpListener result){
        List<Map<String,Object>> mlist=new ArrayList<Map<String,Object>>();
        final Map<String,Object> map=new HashMap<String,Object>();
        map.put("type",type);
        map.put("page",page);
        mlist.add(map);

        Observable.fromIterable(mlist)
                .subscribeOn(Schedulers.io())
                .map(new Function<Map<String,Object>, List<EZDeviceInfo>>() {

                    @Override
                    public List<EZDeviceInfo> apply(@NonNull Map<String, Object> maps) throws Exception {
                        int mLoadType=(int)map.get("type");
                        int page = (int)map.get("page");

                        List<EZDeviceInfo> result = null;
                        if (mLoadType == Static.LOAD_MY_DEVICE) {
                            result = App.getOpenSDK().getDeviceList(page, Static.rows);//getOpenSDK
                        } else if (mLoadType == Static.LOAD_SHARE_DEVICE) {
                            result = App.getOpenSDK().getSharedDeviceList(page, Static.rows);
                        }

                        return result;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<EZDeviceInfo>>(){

                    @Override
                    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.annotations.NonNull List<EZDeviceInfo> s) {
                        result.success(s);
                    }

                    @Override
                    public void onError(@io.reactivex.annotations.NonNull Throwable e) {
                        result.fail("网络异常,加载失败");
                    }

                    @Override
                    public void onComplete() {}
                });

    }
}
